package unipi.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.ErrorCodes;
import unipi.database.User;
import unipi.helpers.LoginCookie;

public class DeleteContact extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public DeleteContact()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String cid = request.getParameter("delcontact");
			
			ErrorCodes ec = User.deleteContact(uid, cid);
			
			String message;
			
			if(ec != ErrorCodes.OK) message = "Παρουσιάστηκε πρόβλημα κατα την διαγραφή της επαφής.";
			else message = "Η επαφή διαγράφηκε με επιτυχία";
			
			String[] msg = new String[]{"", "", "", message};
			request.setAttribute("message", msg);
			RequestDispatcher rq = request.getRequestDispatcher("Profile");
			rq.forward(request, response);	
			
		}

	}
	

}
